#!/usr/bin/env python
# usage:
#   cat ../log/2*.log | grep "KEEP ALIVE" -A 1 | ./update_rate.py
import sys
import operator

stat = {}

prev_time = 0
prev_dur = 0
duration = 0
err_dur = 0

for line in sys.stdin:
  if line[0] == '{':
    if line[1] != '}':
      try:
        d = eval(line)
        for k, v in d.iteritems():
          if k in stat:
            stat[k] += v
          else:
            stat[k] = v
        duration += prev_dur
      except Exception:
        err_dur += prev_dur
  elif line[0] == 'L':
    seg = line.split(' ')
    cur_time = int(seg[3])
    prev_dur = cur_time - prev_time
    prev_time = cur_time

print "Duration: %ds %dh %dd %dm" % (
    duration, duration / 3600, duration / (3600*24), duration / (3600*24*30))
print "ErrorDuration: %ds %dh %dd %dm" % (
    err_dur, err_dur / 3600, err_dur / (3600*24), err_dur / (3600*24*30))
stat = sorted(stat.iteritems(), key=operator.itemgetter(0))
s = [0, 0]
for kv in stat:
  s[0] += kv[0] * kv[1]
  s[1] += kv[1]
  #print "%3d %10d" % (kv[0], kv[1])
print "AvgDelay %10lf ,UpdateCount %10d ,Duration %10d ,UpdateRate %10lf" % (
    s[0] / s[1], s[1], duration, s[1] / duration)
